<%@page import="info.fernandoguillen.javadb2.MessageManager"%>
<%@page import="info.fernandoguillen.javadb2.Message"%>

<%
	if( request.getParameter("texto") != null ){
		MessageManager.insertMessage( request.getParameter("texto") );
	}
%>

<%
	if( request.getParameter("borrar") != null ){
		MessageManager.removeMessageById( new Long(request.getParameter("borrar")) );
	}
%>



<html>
	<head><title>mensajes</title>
	<style>
		body{ margin-left: 20px; }
		textarea{
			border: 1px solid black;
			width: 400px;
			height: 100px;
			font-size: 2em;
			font-weight: bold;
		}
		
		ul{
			list-style: none;
		}
		
		li{
			border: 1px solid grey;
			width: 20em;
			
			padding: 10px;
			margin: 10px;
		}
		
		li a{
			display: block;
			margin-top: 20px;
			padding: 10px;
			background-color: black;
			color: white;
			text-align: right;
		
		}
	</style>
	</head>
	<body>
		
		
		<%
			java.util.List lista = MessageManager.getAll();
			
			out.print( "<h1>" + lista.size() + " mensajes. </h1>" );
			
			out.print( "<ul>" );
			for( int n=0; n<lista.size(); n++){
				Message message = (Message)lista.get(n);
				out.print("<li>" + message.getText() + "<a href=\"./index.jsp?borrar=" + message.getId() + "\"> borrar </a></li>");
			}
		%>
		</ul>

		<p>
			<form action="./index.jsp">
				<textarea name="texto"></textarea><br />
				<input type="submit" value="enviar"/>
			</form>
		</p>
		
	</body>
</html>